www.gusucode.com > 24Beta 虚拟主机版 1.0.0 Beta源码程序 > 24Beta 虚拟主机版 1.0.0 Beta源码程序/24Beta-1.0.0-vhost/protected/modules/admin/controllers/PostController.php

    <?php

class PostController extends CController
{
    public function actionToday()
	{
	    $date = getdate();
	    $start = mktime(0, 0, 0, $date['mon'], $date['mday'], $date['year']);
	    $end = mktime(0, 0, 0, $date['mon'], $date['mday'] + 1, $date['year']);
	    
		$criteria = $this->_getCriteria();
		$criteria->condition = sprintf('(contribute_time between %d and  %d) or (post_time between %d and %d)', $start, $end, $start, $end);
		
	    $pages = $this->_getPages($criteria);
		
		$posts = Post::model()->findAll($criteria);
		
		$this->render('list', array('posts'=>$posts, 'pages'=>$pages));
	}
	
	
	public function actionUnVerify()
	{
		$data = $this->fetchPosts('isauth', Post::NO);
		$this->render('list', $data);
	}
	
	public function actionHide()
	{
		$data = $this->fetchPosts('isshow', Post::NO);
		$this->render('list', $data);
	}
	
	public function actionHot()
	{
		$data = $this->fetchPosts('ishot', Post::YES);
		$this->render('list', $data);
	}
	
	public function actionTop()
	{
		$data = $this->fetchPosts('istop', Post::YES);
		$this->render('list', $data);
	}
	
    public function actionOriginal()
	{
		$data = $this->fetchPosts('isoriginal', Post::YES);
		$this->render('list', $data);
	}
	
    public function actionLocked()
	{
		$data = $this->fetchPosts('allow_comment', Post::NO);
		$this->render('list', $data);
	}
	
	public function actionRecommend()
	{
	    $data = $this->fetchPosts('editor_recommend', Post::YES);
		$this->render('list', $data);
	}
	
	public function actionAll()
	{
		$criteria = $this->_getCriteria();
		
	    $pages = $this->_getPages($criteria);
		
		$posts = Post::model()->findAll($criteria);
		
		$this->render('list', array('posts'=>$posts, 'pages'=>$pages));
	}
	
	private function _getCriteria()
	{
		$criteria = new CDbCriteria();
		$criteria->select = 'id, subject, isoriginal, isauth, isshow, istop, ishot, editor_recommend, category_id, topic_id, source, summary, post_time, post_user, visit_nums, digg_nums, comment_nums, mark1, mark1_nums, mark2, mark2_nums, contributor, contributor_homepage, contribute_time, allow_comment';
		$criteria->limit = 20;
		$criteria->order = 'update_time desc, id desc';
		return $criteria;
	}
	
	private function _getPages($criteria)
	{
		$pages = new CPagination(Post::model()->count($criteria));
		$pages->pageSize = 20;
		$pages->applyLimit($criteria);
		return $pages;
	}

	/**
	 * Deletes a particular model.
	 */
	public function actionDelete()
	{
	    if (app()->request->isAjaxRequest && app()->request->isPostRequest) {
			$aid = isset($_GET['aid']) ? (int)$_GET['aid'] : null;
			$aids = isset($_POST['chkpost']) ? (int)$_POST['chkpost'] : null;
			if (!empty($aid) && is_numeric($aid)) {
    			$result = Post::model()->findByPk($aid)->delete();
    			$data['result'] = $result ? 1 : 0;
    			$data['message'] = '删除' . ($result ? '成功' : '失败');
			} elseif (count($aids) > 0) {
				$strids = '(`' . implode('`, `', $aids) . '`)';
			    $result = Post::model()->deleteAll("`id` in $strids");
			    $data['result'] = $result ? 1 : 0;
    			$data['message'] = '删除' . ($result ? '成功' : '失败');
			} else {
			    $data['result'] = -1;
    			$data['message'] = '参数格式错误';
			}
		}
		else {
			$data['result'] = -1;
    		$data['message'] = '非法请求';
		}
		echo json_encode($data);
	}
	
	
	public function actionEdit()
	{
	    if (app()->request->isPostRequest) {
	        $result = $this->updatePost();
	        $postId = (int)$_POST['postid'];
	        $subject = trim($_POST['subject']);
	        $returnUrl = $_POST['urlReferrer'];
	        if ($result >= 0 && is_int($result)) {
	            $prompt['result'] = true;
	            $prompt['note'][] = array('text' => '文章更新成功,点击查看 - ' . $subject, 'target'=>'_blank', 'url' => url('post/show', array('aid' => $postId)));
	            $prompt['note'][] = array('text' => '返回文章列表', 'url'=> $returnUrl);
	        } else {
	            $prompt['result'] = false;
	            $prompt['note'][] = array('text' => '文章更新失败- ' . $subject);
	            $prompt['note'][] = array('text' => '返回文章列表', 'url'=> $returnUrl);
	            $prompt['note'][] = array('text' => '返回重试', 'url'=>'javascript:history.back();');
	        }
	        $this->render('/_public/prompt', array('prompt'=>$prompt));
	        exit(0);
	    }
	    
	    
	    $pid = (int)$_GET['aid'];
	    $post = Post::model()->findByPk($pid);
	    if ($post == null) {
	        throw new CHttpException(404, '此文章存不存在');
	    }
	    $tabs = array(
            'tab1'=>array(
        		'title'=>'文章内容',
        		'view'=>'edit_content',
            ),
            'tab2'=>array(
        		'title'=>'文章概述',
        		'view'=>'edit_summary',
            ),
            'tab3'=>array(
        		'title'=>'其它内容',
        		'view'=>'edit_other',
            ),
            'tab4' => array(
                'title' => '标题:' . CHtml::encode($post->subject),
                'url' => 'javascript:void(0);',
            ),
        );
        $categorys = Category::model()->getIdNamePairs();
		$topics = Topic::model()->getIdNamePairs();
		
	    $this->render('edit', array(
	    	'post' => $post,
	        'tabs' => $tabs,
	        'categorys' => $categorys,
	        'topics' => $topics,
	    ));
	}
	
	
	private function updatePost($data = null)
	{
	    if ($data === null) $data = $_POST;
	    
	    $postId = $data['postid'];
	    unset($data['postid']);
	    $result = Post::model()->updateByPk($postId, $data);
	    return $result;
	}
	
	
	private function fetchPosts($field, $value)
	{
	    $criteria = $this->_getCriteria();
		$criteria->condition = "$field = $value";
		
	    $pages = $this->_getPages($criteria);
		
		$posts = Post::model()->findAll($criteria);
		
		$data = array('posts' => $posts, 'pages' => $pages);
		return $data;
	}
}